package com.live.shop.home;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.live.shop.BaseActivity;
import com.live.shop.R;
import com.live.shop.home.prologue.AddPrologueActivity;
import com.live.shop.home.prologue.BatchProloguesActivity;
import com.live.shop.model.PrologueModel;
import com.live.shop.sql.PrologueListDatabase;
import com.live.shop.tool.StatusBarUtils;

import java.util.List;

public class ProloguesListActivity extends BaseActivity {
    RecyclerView prologues_recyclerview;
    ImageButton prologues_back;
    TextView prologues_batchadd;
    TextView prologues_add;
    List<PrologueModel> list;
    PrologueListDatabase prologueDb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prologues_list);
        StatusBarUtils.setDarkStatusWhite(this,R.color.white);

        prologues_back = findViewById(R.id.prologues_back);
        prologues_batchadd = findViewById(R.id.prologues_batchadd);
        prologues_add = findViewById(R.id.prologues_add);
        prologues_recyclerview = findViewById(R.id.prologues_recyclerview);

        prologues_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        prologues_batchadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(), BatchProloguesActivity.class));
            }
        });
        prologues_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(), AddPrologueActivity.class));
            }
        });
    }
    public void initData(){
        prologueDb = new PrologueListDatabase(getApplicationContext());
        list = prologueDb.selectAll();
        adapter = new MyAdapter();
        prologues_recyclerview.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        prologues_recyclerview.setAdapter(adapter);
    }

    private MyAdapter adapter;

    class MyAdapter extends RecyclerView.Adapter {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyAdapter.MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_prologue, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
            MyAdapter.MyViewHolder mholder = (MyAdapter.MyViewHolder) holder;
            PrologueModel prologueModel = list.get(position);

            if(prologueModel.getPrologueStatus()==1){
                mholder.item_prologue_status.setSelected(true);
                mholder.item_prologue_status_txt.setText("启用中");
                mholder.item_prologue_status_txt.setTextColor(getColor(R.color.blue));
            }else{
                mholder.item_prologue_status.setSelected(false);
                mholder.item_prologue_status_txt.setText("未启用");
                mholder.item_prologue_status_txt.setTextColor(getColor(R.color.gary));
            }

            mholder.item_prologue_text.setText(prologueModel.getPrologueTxt().isEmpty()?"未设置文案":prologueModel.getPrologueTxt());
            mholder.item_prologue_use_num.setText(String.valueOf(prologueModel.getUseNum()));
            mholder.item_prologue_create_time.setText(prologueModel.getCreateTime());
            mholder.item_prologue_status.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(prologueModel.getPrologueStatus() == 1){
                        //关闭启用
                        list.get(position).setPrologueStatus(0);
                        prologueDb.updatePrologueStatus(prologueModel.getPrologueId(),0);
                    }else{
                        //开启启用
                        list.get(position).setPrologueStatus(1);
                        prologueDb.updatePrologueStatus(prologueModel.getPrologueId(),1);
                    }
                    notifyDataSetChanged();
                }
            });
            mholder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getApplicationContext(), AddPrologueActivity.class);
                    intent.putExtra("data",prologueModel);
                    intent.putExtra("type", 1);
                    startActivity(intent);
                }
            });
            mholder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    dialog_item_delete(holder.getBindingAdapterPosition());
                    return true;
                }
            });
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView item_prologue_text;
            ImageView item_prologue_status;
            TextView item_prologue_status_txt;
            TextView item_prologue_use_num;
            TextView item_prologue_create_time;

            public MyViewHolder(View itemView) {
                super(itemView);
                item_prologue_text = itemView.findViewById(R.id.item_prologue_text);
                item_prologue_status = itemView.findViewById(R.id.item_prologue_status);
                item_prologue_status_txt = itemView.findViewById(R.id.item_prologue_status_txt);
                item_prologue_use_num = itemView.findViewById(R.id.item_prologue_use_num);
                item_prologue_create_time = itemView.findViewById(R.id.item_prologue_create_time);
            }
        }
    }
    public void dialog_item_delete(int position){
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setCancelable(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_item_delete);
        final TextView diaglog_delete_ok = window.findViewById(R.id.diaglog_delete_ok);
        final TextView diaglog_delete_no = window.findViewById(R.id.diaglog_delete_no);
        diaglog_delete_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adapter.notifyItemRemoved(position);
                prologueDb.deleteData(list.get(position).getPrologueId());
                list.remove(position);
                dialog.dismiss();
            }
        });
        diaglog_delete_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}